package com.xgkj.diyiketang.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xgkj.diyiketang.ProtocolConst;
import com.xgkj.diyiketang.data.MyOrderInfo;
import com.xgkj.diyiketang.http.CouponHttpRequest;
import com.xgkj.diyiketang.http.CouponHttpResponse;
import com.xgkj.diyiketang.http.engine.CouponHttpBaseTask;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by windo on 16/4/12.
 */
public class MyOrderTask extends CouponHttpBaseTask{
    private int mPage;
    private int mSize;

    public MyOrderTask(int pageNo,int size) {
        super(TaskType.MY_ORDER_TASK);
        mPage = pageNo;
        mSize = size;
    }

    @Override
    protected void processResponseData(CouponHttpResponse response) {
        JSONObject json = response.getBody();
        int currentPage;
        int pageCount;
        int nextPage = 0;
        List<MyOrderInfo> list = new ArrayList<MyOrderInfo>();
        if (json != null) {
            JSONObject listObject = json.getJSONObject("transactionList");

            if (listObject != null) {
                currentPage = listObject.getIntValue(ProtocolConst.CURRENT_PAGE);
                pageCount = listObject.getInteger(ProtocolConst.PAGE_COUNT);
                nextPage = currentPage>=pageCount?0:(currentPage+1);
                list = JSON.parseArray(listObject.getString(ProtocolConst.LIST_TAG),MyOrderInfo.class);
            }
        }
        notifyTaskCompleted(nextPage,list);
    }

    @Override
    protected void prepareRequest() {
        super.prepareRequest();
        if (mPhase == TASK_PHASE_INIT){
            CouponHttpRequest request = new CouponHttpRequest(RequestType.MYORDERLIST_REQUEST);
            JSONObject json = new JSONObject();
            json.put("pageNo",mPage);
            json.put("size",mSize);
            request.setData(json.toString());
            request.setApiNo(ApiNo.MYORDERLIST);
            addRequest(request);
        }
    }
}
